package com.huawei.hwid.update.d;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build.VERSION;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.KeyEvent;
import com.huawei.hms.support.api.entity.hwid.HwIDConstant.ACTION;
import com.huawei.hwid.core.d.b.e;
import com.huawei.hwid.f.d;
import com.huawei.hwid.update.a.a.a;
import com.huawei.hwid.update.a.a.b;
import com.huawei.hwid.update.a.a.c;
import com.huawei.hwid.update.provider.UpdateProvider;
import com.huawei.updatesdk.UpdateSdkAPI;
import com.huawei.updatesdk.service.appmgr.bean.ApkUpgradeInfo;
import com.huawei.updatesdk.service.otaupdate.CheckUpdateCallBack;
import com.huawei.updatesdk.service.otaupdate.UpdateKey;
import java.io.File;

public class k extends a implements b {
    private a j;
    private c k;
    private int l = 0;

    private static Uri a(Context context, File file) {
        Object obj = 1;
        d dVar = new d(context);
        String packageName = context.getPackageName();
        String str = packageName + ".hwid.update.provider";
        if (VERSION.SDK_INT <= 23 || (context.getApplicationInfo().targetSdkVersion <= 23 && !dVar.a(packageName, str))) {
            obj = null;
        }
        if (obj != null) {
            return UpdateProvider.a(context, str, file);
        }
        return Uri.fromFile(file);
    }

    private static void b(final b bVar, final int i, final c cVar) {
        if (bVar != null) {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                public void run() {
                    bVar.a(i, cVar);
                }
            });
        }
    }

    public void a(Activity activity) {
        super.a(activity);
        if (this.c != null) {
            this.f = 6;
            if (!this.c.e() || TextUtils.isEmpty(this.h)) {
                a(c.class);
                a((b) this);
                return;
            }
            a(g.class);
        }
    }

    public void a() {
        i();
        super.a();
    }

    public int f() {
        return 2006;
    }

    public boolean a(int i, int i2, Intent intent) {
        if (this.e && this.b != null) {
            return this.b.a(i, i2, intent);
        }
        if (this.f != 6 || i != f()) {
            return false;
        }
        if (a(this.g, this.i)) {
            a(0, this.f);
        } else {
            g();
        }
        return true;
    }

    public void a(int i, c cVar) {
        e.b("UpdateWizard", "onCheckUpdate with status: " + i, true);
        switch (i) {
            case 1000:
                this.k = cVar;
                a(e.class);
                h();
                return;
            case 1201:
            case 1203:
                a(h.b.class);
                return;
            case 1202:
                e();
                a(8, 0);
                return;
            default:
                a(h.b.class);
                return;
        }
    }

    public void a(int i, int i2, int i3, File file) {
        int i4 = 0;
        e.b("UpdateWizard", "onDownloadPackage, status: " + i + " , and localFile is null? " + (file == null), true);
        switch (i) {
            case 2000:
                e();
                if (file == null) {
                    g();
                    return;
                } else {
                    a(file);
                    return;
                }
            case 2100:
                if (this.d != null && (this.d instanceof e)) {
                    if (i2 >= 0 && i3 > 0) {
                        i4 = (int) ((((long) i2) * 100) / ((long) i3));
                    }
                    this.l = i4;
                    ((e) this.d).b(i4);
                    return;
                }
                return;
            case 2201:
                a(h.c.class);
                return;
            case 2202:
                a(d.b.class);
                return;
            case 2203:
            case 2204:
                a(h.d.class);
                return;
            default:
                return;
        }
    }

    private void a(File file) {
        e.b("UpdateWizard", "startInstaller.", true);
        Context d = d();
        if (d != null && !d.isFinishing()) {
            Uri a = a(d, file);
            if (a == null) {
                e.d("UpdateWizard", "In startInstaller, Failed to creates a Uri from a file.", true);
                g();
                return;
            }
            Intent intent = new Intent(ACTION.HWID_SCHEME_URL);
            intent.setDataAndType(a, "application/vnd.android.package-archive");
            intent.setFlags(3);
            try {
                d.startActivityForResult(intent, f());
            } catch (ActivityNotFoundException e) {
                e.d("UpdateWizard", "In startInstaller, Failed to start package installer.", true);
                g();
            }
        }
    }

    public void a(b bVar) {
        e.b("UpdateWizard", "Enter onCancel.", true);
        if (bVar instanceof g) {
            c();
        } else if (bVar instanceof c) {
            i();
            c();
        } else if (bVar instanceof e) {
            i();
            a(d.c.class);
        } else if (bVar instanceof d.c) {
            a(e.class);
            h();
        } else if (bVar instanceof d.b) {
            c();
        } else {
            g();
        }
    }

    public void b(b bVar) {
        e.b("UpdateWizard", "Enter onDoWork.", true);
        if (bVar instanceof g) {
            bVar.c();
            a(c.class);
            a((b) this);
        } else if (bVar instanceof d.c) {
            bVar.c();
            c();
        } else if (bVar instanceof d.b) {
            a(e.class);
            h();
        } else if (bVar instanceof h.b) {
            g();
        } else if (bVar instanceof h.c) {
            g();
        } else if (bVar instanceof h.d) {
            g();
        }
    }

    private void g() {
        if (!a(false)) {
            a(8, this.f);
        }
    }

    void a(Class<? extends b> cls) {
        e();
        try {
            b bVar = (b) cls.newInstance();
            if (!TextUtils.isEmpty(this.h) && (bVar instanceof g)) {
                ((g) bVar).a(this.h);
            }
            if (this.l > 0 && (bVar instanceof e)) {
                ((e) bVar).a(this.l);
            }
            bVar.a((a) this);
            this.d = bVar;
        } catch (InstantiationException e) {
            e.d("UpdateWizard", "InstantiationException In showDialog, Failed to show the dialog.", true);
        } catch (IllegalAccessException e2) {
            e.d("UpdateWizard", "IllegalAccessException In showDialog, Failed to show the dialog.", true);
        } catch (IllegalStateException e3) {
            e.d("UpdateWizard", "IllegalStateException In showDialog, Failed to show the dialog.", true);
        }
    }

    void c() {
        a(13, this.f);
    }

    private void a(final b bVar) {
        if (bVar != null) {
            Context d = d();
            if (d == null || d.isFinishing()) {
                b(bVar, 1201, null);
            } else {
                UpdateSdkAPI.checkTargetAppUpdate(d, this.c.a(), new CheckUpdateCallBack(this) {
                    final /* synthetic */ k b;

                    public void onUpdateInfo(Intent intent) {
                        if (intent != null) {
                            try {
                                int intExtra = intent.getIntExtra(UpdateKey.STATUS, -99);
                                e.b("UpdateWizard", "onUpdateInfo status", true);
                                if (intExtra == 7) {
                                    ApkUpgradeInfo apkUpgradeInfo = (ApkUpgradeInfo) intent.getSerializableExtra(UpdateKey.INFO);
                                    if (apkUpgradeInfo != null) {
                                        String package_ = apkUpgradeInfo.getPackage_();
                                        int versionCode_ = apkUpgradeInfo.getVersionCode_();
                                        Object downurl_ = apkUpgradeInfo.getDownurl_();
                                        int size_ = apkUpgradeInfo.getSize_();
                                        Object sha256_ = apkUpgradeInfo.getSha256_();
                                        if (TextUtils.isEmpty(package_) || !package_.equals(this.b.c.a())) {
                                            k.b(bVar, 1201, null);
                                        } else if (versionCode_ < this.b.c.b()) {
                                            k.b(bVar, 1203, null);
                                        } else if (TextUtils.isEmpty(downurl_) || TextUtils.isEmpty(sha256_)) {
                                            k.b(bVar, 1201, null);
                                        } else {
                                            k.b(bVar, 1000, new c(package_, versionCode_, downurl_, size_, sha256_));
                                        }
                                    }
                                } else if (intExtra == 3) {
                                    k.b(bVar, 1202, null);
                                } else {
                                    k.b(bVar, 1201, null);
                                }
                            } catch (Exception e) {
                                e.d("UpdateWizard", "intent has some error", true);
                                k.b(bVar, 1201, null);
                            }
                        }
                    }

                    public void onMarketInstallInfo(Intent intent) {
                    }

                    public void onMarketStoreError(int i) {
                    }

                    public void onUpdateStoreError(int i) {
                    }
                });
            }
        }
    }

    private void h() {
        Context d = d();
        if (d == null || d.isFinishing()) {
            a(h.c.class);
            return;
        }
        i();
        this.j = new com.huawei.hwid.update.a.c(new com.huawei.hwid.update.a.d(d));
        this.j.a(this, this.k);
    }

    private void i() {
        if (this.j != null) {
            this.j.a();
            this.j = null;
        }
    }

    public void a(int i, KeyEvent keyEvent) {
        if (this.e && this.b != null) {
            this.b.a(i, keyEvent);
        } else if (4 == i) {
            e.b("UpdateWizard", "In onKeyUp, Call finish.", true);
            Activity d = d();
            if (d != null && !d.isFinishing()) {
                d.setResult(0, null);
                d.finish();
            }
        }
    }
}
